{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 怎样使用uwsgi在centos7上安装Python3的Flask在线Web服务"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "查看版本：  \n",
    "cat /etc/redhat-release  \n",
    "CentOS Linux release 7.7.1908 (Core)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 1、添加一个用户"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "新增用户：  \n",
    "adduser antpython"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "修改密码：  \n",
    "passwd antpython"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "添加到sudo用户组：  \n",
    "gpasswd -a antpython wheel"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "切换到antpython用户：  \n",
    "sudo -iu antpython"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "注意：下方所有的命令，都是切换到了antpython用户进行的，所以很多都带上了sudo"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 2、初始化环境"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "初始化仓库：  \n",
    "sudo yum install epel-release"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "安装gcc和ngnix：  \n",
    "sudo yum install gcc nginx"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 3、安装anaconda"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "下载地址在：https://www.anaconda.com/distribution/"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "下载Anaconda的安装文件：   \n",
    "wget https://repo.anaconda.com/archive/Anaconda3-2019.10-Linux-x86_64.sh"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "安装Anaconda：  \n",
    "sh Anaconda3-2019.10-Linux-x86_64.sh \n",
    "\n",
    "路径选择的时候，不要改动，默认是：  \n",
    "/home/antpython/anaconda3"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 4、创建python虚拟环境"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "激活anaconda：   \n",
    "source anaconda3/bin/activate"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "安装virtualenv：  \n",
    "pip install virtualenv"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "创建目录：  \n",
    "mkdir ~/myproject  \n",
    "cd ~/myproject"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "创建虚拟环境目录：  \n",
    "virtualenv myprojectenv"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "激活新创建的虚拟环境：  \n",
    "source myprojectenv/bin/activate"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 5、初始化一个简单地flask应用"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "安装flask和uwsgi：  \n",
    "pip install uwsgi flask"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "创建一个flask文件：  \n",
    "vim ~/myproject/myproject.py"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<pre>\n",
    "# 把这段代码粘贴进去\n",
    "\n",
    "from flask import Flask\n",
    "application = Flask(__name__)\n",
    "\n",
    "@application.route(\"/\")\n",
    "def hello():\n",
    "    return \"Hello There!\"\n",
    "\n",
    "if __name__ == \"__main__\":\n",
    "    application.run(host='0.0.0.0')\n",
    "</pre>"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "回到命令行，启动测试flask服务：  \n",
    "python myproject.py"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "打开系统的5000端口号  \n",
    "sudo firewall-cmd --permanent --add-port=5000/tcp  \n",
    "sudo firewall-cmd --reload  "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "在浏览器访问，即可打开网页，其中的xx.xx.xx.xx换成你的ip：    \n",
    "http://xx.xx.xx.xx:5000"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 6、创建一个wsgi入口"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "vim ~/myproject/mywsgi.py"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<pre>\n",
    "# 粘贴代码如下： \n",
    "from myproject import application\n",
    "\n",
    "if __name__ == \"__main__\":\n",
    "    application.run()\n",
    "</pre>"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "使用这个命令测试下uwsgi  \n",
    "uwsgi --socket 0.0.0.0:5000 --protocol=http -w mywsgi"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "在浏览器访问，即可打开网页，其中的xx.xx.xx.xx换成你的ip：    \n",
    "http://xx.xx.xx.xx:5000"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 7、配置uwsgi的配置文件"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "编辑一个配置文件  \n",
    "vim /home/antpython/myproject/myproject.ini"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<pre>\n",
    "[uwsgi]\n",
    "module = mywsgi\n",
    "\n",
    "master = true\n",
    "processes = 5\n",
    "threads = 100\n",
    "\n",
    "http = 0.0.0.0:5000\n",
    "virtualenv = /home/antpython/myproject/myprojectenv\n",
    "die-on-term = true\n",
    "\n",
    "</pre>"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "回到命令行，通过以下命令启动一个uwsgi服务器：  \n",
    "uwsgi --ini myproject.ini"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 8、创建自启动Systemd配置"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "好处是：系统自己重启后，服务会自动启动"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "sudo vim /etc/systemd/system/myproject.service"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<pre>\n",
    "# 输入下面的内容\n",
    "[Unit]\n",
    "Description=uWSGI instance to serve myproject\n",
    "After=network.target\n",
    "\n",
    "[Service]\n",
    "User=antpython\n",
    "Group=nginx\n",
    "WorkingDirectory=/home/antpython/myproject\n",
    "Environment=\"PATH=/home/antpython/myproject/myprojectenv/bin\"\n",
    "ExecStart=/home/antpython/myproject/myprojectenv/bin/uwsgi --ini myproject.ini\n",
    "\n",
    "[Install]\n",
    "WantedBy=multi-user.target\n",
    "</pre>"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "启动服务  \n",
    "sudo systemctl start myproject.service\n",
    "\n",
    "开机自动启动服务  \n",
    "sudo systemctl enable myproject.service"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 9、参考资料：\n",
    "\n",
    "* https://www.digitalocean.com/community/tutorials/how-to-serve-flask-applications-with-uwsgi-and-nginx-on-centos-7\n",
    "* https://www.kingname.info/2019/07/08/deploy-flask-uwsgi-without-nginx/"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
